package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * 用户信息业务逻辑处理层
 */
@Service
public class SettingService {

    @Reference
    private BlackListApi blackListApi;

    @Reference
    private QuestionApi questionApi;

    /**
     * 分页查询黑名单
     */
    public PageResult<UserInfo> queryPageByUserId(Long page, Long pagesize) {
        return blackListApi.queryPageByUserId(page, pagesize, UserHolder.getUserId());
    }

    /**
     * 黑名单删除
     * /blacklist/{uid}: 可以随便写
     */
    public void deleteById(Long blackUserId) {
        blackListApi.deleteById(blackUserId,UserHolder.getUserId());
    }

    /**
     * 设置陌生人问题 - 保存 或 修改
     */
    public void saveOrUpdateQuestion(String content) {
        Long userId = UserHolder.getUserId();
        //1.根据当前用户id查询问题表 记录是否存在
        Question question = questionApi.queryByUserId(userId);
        //2.如果不存在则调用服务保存问题表
        if(question == null){
            question = new Question();
            question.setTxt(content);//陌生人问题
            question.setUserId(userId);//当前用户id
            questionApi.saveQuestion(question);
        }else {
            //3.如果存在则调用服务更新问题表
            question.setTxt(content);
            questionApi.updateById(question);
        }

    }
}
